package com.fy56.platform.goodsTracking.web.restful.api.kafkaListener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fy56.platform.goodsTracking.commons.DateUtil;
import com.fy56.platform.goodsTracking.service.GoodstrackedRecordPublicServie;
import com.fy56.platform.goodsTracking.service.GoodstrackedRecordTopPublicService;
import com.fy56.platform.goodsTracking.web.restful.api.model.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.listener.MessageListener;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by sxg on 2019/11/26.
 */
public class KafkaConsumerListener implements MessageListener<String, String> {
    private static final Logger logger = LoggerFactory.getLogger(KafkaConsumerListener.class);

    @Autowired
    private GoodstrackedRecordTopPublicService topService;
    @Autowired
    private GoodstrackedRecordPublicServie subService;

    @SuppressWarnings("Duplicates")
    @Override
    public void onMessage(ConsumerRecord<String, String> records) {
        String json = records.value().toString();
        String topic = records.topic().toString();
        if (StringUtils.isEmpty(json)) {
            return;
        }
        logger.info("--------------------" + topic + "------------" + new Date());
        logger.info("messageValue:"+json);
        ResultModel r = null;
        if (topic.equals("f6GoodsTrackingNotifyQueue")) {
            try {
                Map<String, Object> jsonMap = JSON.parseObject(json, Map.class);
                Map<String, Object> messageMap = (Map)jsonMap.get("messageObject");
                Map<String, String> reqParam = new HashMap<String, String>();
                Map<String, String> data = new HashMap<String, String>();
                String recordid= jsonMap.get("Recordid").toString();
                String consignmentbillmasterid= jsonMap.get("consignmentbillmasterid").toString();
                String trackingNo = jsonMap.get("messageId").toString();
                String code = messageMap.get("code").toString();
                String message ="";

                if(recordid!=null && !recordid.isEmpty()){
                    //更新货物跟踪信息
                    subService.updateTyCallback(recordid,consignmentbillmasterid,code,message);
                } else {
                    //更新订单信息
                    topService.updateTyCallback(consignmentbillmasterid,code,message);
                }

            } catch (Exception e) {
                e.printStackTrace();
                logger.error("货物跟踪通知通用队列消费者错误", e);
            }
        }else if (topic.equals("f6CarNotifyQueue")) {
            try {
                Map<String, Object> jsonMap = JSON.parseObject(json, Map.class);
                Map<String, Object> messageMap = (Map)jsonMap.get("messageObject");
                Map<String, String> reqParam = new HashMap<String, String>();
                Map<String, String> data = new HashMap<String, String>();
                String recordid= jsonMap.get("Recordid").toString();
                String consignmentbillmasterid= jsonMap.get("consignmentbillmasterid").toString();
                String trackingNo = jsonMap.get("messageId").toString();
                String code = messageMap.get("code").toString();
                String message = "";

                //更新car
                topService.updateCarCallback(consignmentbillmasterid,code,message);

            } catch (Exception e) {
                e.printStackTrace();
                logger.error("car通知通用队列消费者错误", e);
            }
        }else if (topic.equals("f6DshkNotifyQueue")) {
            try {
                Map<String, Object> jsonMap = JSON.parseObject(json, Map.class);
                Map<String, Object> messageMap = (Map)jsonMap.get("messageObject");
                Map<String, String> reqParam = new HashMap<String, String>();
                Map<String, String> data = new HashMap<String, String>();
                String recordid= jsonMap.get("Recordid").toString();
                String consignmentbillmasterid= jsonMap.get("consignmentbillmasterid").toString();
                String trackingNo = jsonMap.get("messageId").toString();
                String code = messageMap.get("code").toString();
                String message = "";

                //更新car
                topService.updateDshkCallback(consignmentbillmasterid,code,message);

            } catch (Exception e) {
                e.printStackTrace();
                logger.error("货物跟踪通知通用队列消费者错误", e);
            }
        }
        json=null;
        topic=null;

    }


    /**
     * @param throwable
     * @return
     */
    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            throwable.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    }

    public String getJosnVal(JSONObject jsonObject, String fieldName) {
        return jsonObject.get(fieldName) == null ? "" : jsonObject.get(fieldName).toString();
    }


}
